package com.tang.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tang.reggie.common.R;
import com.tang.reggie.dto.OrdersDto;
import com.tang.reggie.pojo.Orders;
import com.tang.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders){
        log.info("订单模块：下单orders:{}",orders);
        ordersService.add(orders);
        return R.success("添加成功");
    }

    /**
     * 前台订单展示
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R page(int page,int pageSize){
        log.info("订单模块：前台订单展示");
        Page<OrdersDto> ordersPage=ordersService.getPage(page,pageSize);
        return R.success(ordersPage);
    }

    /**
     * 后台订单分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R page(int page, int pageSize, Long number, LocalDateTime beginTime,LocalDateTime endTime){
        log.info("订单模块：后台订单分页查询");
        Page<Orders> orders=ordersService.getAll(page,pageSize,number,beginTime,endTime);
        return R.success(orders);
    }

    /**
     * 订单状态修改
     * @param orders
     * @return
     */
    @PutMapping
    public R order(@RequestBody Orders orders){
        log.info("订单模块：订单派送");
        ordersService.updateById(orders);
        return R.success("订单派送成功");
    }

    /**
     * 再来一单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R again(@RequestBody Orders orders){
        log.info("订单模块：再来一单功能");
        ordersService.again(orders);
        return R.success("再来一单添加成功");
    }
}
